package com.hit.eduService.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hit.eduService.entity.EduCourse;
import com.hit.eduService.entity.EduCourseDescription;
import com.hit.eduService.entity.frontVo.CourseQueryVo;
import com.hit.eduService.entity.frontVo.CourseWebVo;
import com.hit.eduService.entity.vo.CourseInfoVo;
import com.hit.eduService.entity.vo.CoursePublishVo;
import com.hit.eduService.mapper.EduCourseMapper;
import com.hit.eduService.service.EduChapterService;
import com.hit.eduService.service.EduCourseDescriptionService;
import com.hit.eduService.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hit.eduService.service.EduVideoService;
import com.hit.serviceBase.exceptionHandler.MyException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-11-04
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private EduChapterService eduChapterService;

    //添加课程基本信息到数据库的edu_course表和edu_course_description表
    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //把CourseInfoVo对象中的内容加入到两个表中，因为这两个表是一对一关系，所以让它们的主键相同
        //1.先填充edu_course表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        baseMapper.insert(eduCourse);
        //拿到插入到edu_course表的主键
        String id=eduCourse.getId();//调用baseMapper的insert方法会把eduService对象的id也赋值了吗？？？
        //2.填充edu_course_description
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        eduCourseDescription.setDescription(courseInfoVo.getDescription());
        eduCourseDescription.setId(id);
        eduCourseDescriptionService.save(eduCourseDescription);
        return id;
    }

    //根据传入的课程id，返回对应的CourseInfoVo对象(需要查edu_course和edu_course_description两张表)
    @Override
    public CourseInfoVo getCourseInfo(String id) {
        CourseInfoVo ans=new CourseInfoVo();
        //1先查edu_course表
        EduCourse eduCourse = baseMapper.selectById(id);
        BeanUtils.copyProperties(eduCourse,ans);
        //2查询edu_course_description表
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        BeanUtils.copyProperties(eduCourseDescription,ans);
        return ans;
    }

    //课程修改，修改课程id以外的信息，需要更改edu_course表和edu_course_description表
    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //1.修改课程表
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update=baseMapper.updateById(eduCourse);
        if(update==0)throw new MyException(20001,"修改课程信息失败");
        //2.修改课程描述表
        EduCourseDescription eduCourseDescription=new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo,eduCourseDescription);
        eduCourseDescriptionService.updateById(eduCourseDescription);

    }

    @Override
    public CoursePublishVo publishCourseInfo(String id) {
        //调用mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(id);
        return  publishCourseInfo;
    }

    //根据id删除课程，需要删除相关的小节、课程描述、课程、章节
    //需要先删除小节，再删除章节，再删除课程描述，再删除课程
    @Override
    public void deleteCourse(String courseId) {
        //1.删除小节
        //写成eduVideoService.removeById(courseId)是错误的，这样删除的是章节id等于courseId的章节
        eduVideoService.deleteVideoByCourseId(courseId);
        //2.删除章节
        eduChapterService.deleteChapterByCourseId(courseId);
        //3.删除课程描述  因为课程描述和课程是一对一关系，课程的id也是课程描述的主键，所以可以直接调用removeById删除
        eduCourseDescriptionService.removeById(courseId);
        //4.删除课程自身
        int cnt = baseMapper.deleteById(courseId);
        if(cnt==0){//表示没有删除任何一行
            throw new MyException(20001,"删除失败");
        }
    }
    //用于前台的分页条件查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageCourse, CourseQueryVo courseQueryVo) {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(courseQueryVo!=null){
            //判断条件值是否为空，不为空拼接
            if(!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())) { //一级分类
                wrapper.eq("subject_parent_id",courseQueryVo.getSubjectParentId());
            }
            if(!StringUtils.isEmpty(courseQueryVo.getSubjectId())) { //二级分类
                wrapper.eq("subject_id",courseQueryVo.getSubjectId());
            }
            if(!StringUtils.isEmpty(courseQueryVo.getBuyCountSort())) { //关注度
                wrapper.orderByDesc("buy_count");
            }
            if (!StringUtils.isEmpty(courseQueryVo.getGmtCreateSort())) { //最新
                wrapper.orderByDesc("gmt_create");
            }

            if (!StringUtils.isEmpty(courseQueryVo.getPriceSort())) {//价格
                wrapper.orderByDesc("price");
            }
        }
        baseMapper.selectPage(pageCourse,wrapper);

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();//下一页
        boolean hasPrevious = pageCourse.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        //map返回
        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getBaseCourseInfo(courseId);
    }


}
